package com.spzx.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.cart.api.RemoteCartService;
import com.spzx.cart.domain.CartInfo;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.domain.OrderItem;
import com.spzx.order.domain.TradeVo;
import com.spzx.order.mapper.OrderInfoMapper;
import com.spzx.order.service.IOrderInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private RemoteCartService remoteCartService;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public TradeVo trade() {
        //新建tradeVo对象
        TradeVo tradeVo = new TradeVo();
        //远程调用：订单页面显示购物车列表
        Long userId = SecurityContextHolder.getUserId();
        R<List<CartInfo>> cartCheckedListR = remoteCartService.getCartCheckedList(userId, SecurityConstants.INNER);
        List<CartInfo> cartInfoList = cartCheckedListR.getData();
        List<OrderItem> orderItemList=null;
        if(!CollectionUtils.isEmpty(cartInfoList)){
            orderItemList  = cartInfoList.stream().map((cartInfo) -> {
                OrderItem orderItem = new OrderItem();
                BeanUtils.copyProperties(cartInfo, orderItem);
                return orderItem;
            }).collect(Collectors.toList());
        }
        tradeVo.setOrderItemList(orderItemList);

        //封装商品总金额

        //遍历orderItemList，把所有商品的单价*数量加起来就是总价
        BigDecimal skuAllPrice = new BigDecimal(0);
        for (OrderItem orderItem : orderItemList) {
            BigDecimal skuPrice = orderItem.getSkuPrice();
            Integer skuNum = orderItem.getSkuNum();
            BigDecimal bigDecimal = skuPrice.multiply(new BigDecimal(skuNum));
            skuAllPrice = skuAllPrice.add(bigDecimal);

        }
        tradeVo.setTotalAmount(skuAllPrice);

        String tradeNo = this.generateTradeNo(userId);
        tradeVo.setTradeNo(tradeNo);

        return tradeVo;
    }
    //渲染订单确认页面-生成用户流水号
    private String generateTradeNo(Long userId){
        //构建流水号key
        String userTradeKey = "user:tradeNo:"+userId;
        //2 构建流水号value
        String tradeNo = UUID.randomUUID().toString().replaceAll("-","");
        //3 TODO 防止重复提交：将流水号存入redis，暂存五分钟
        redisTemplate.opsForValue().set(userTradeKey,tradeNo,5, TimeUnit.MINUTES);
        return tradeNo;
    }




}
